package cn.water.rocketmq.controller;

import cn.water.rocketmq.service.HelloService;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {
    @Autowired
    RocketMQTemplate rocketMQTemplate;
    @GetMapping("/hello")
    public String sayHi(String name){
        //新建消息
        Message<String> message = MessageBuilder.withPayload(name).build();
        //同步发送消息
        //SendResult result = rocketMQTemplate.syncSend("producer-test-topic:四川", message);
        //发布同步消息，延迟处理
        //rocketMQTemplate.syncSend("producer-test-topic:成都",message,500,5);

        //发送异步消息
        rocketMQTemplate.asyncSend("producer-test-topic:宜宾", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                //消息发送成功
                System.out.println("消息发送成功");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("消息发送失败");
            }
        },500,5);
        System.out.println("消息已发送");
        return "send success";
    }
}
